//
//  main.cpp
//  CppDemo
//
//  Created by zhangjikuan on 2020/8/31.
//  Copyright © 2020 hsgd. All rights reserved.
//

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <iomanip>

using namespace::std;
enum color{red,blue,black};






void math_calculate() {
      // 数字定义
      short  s = 10;
      int    i = -1000;
      long   l = 100000;
      float  f = 230.47;
      double d = 200.374;
    
      // 数学运算
      cout << "sin(d) :" << sin(d) << endl;
      cout << "abs(i)  :" << abs(i) << endl;
      cout << "floor(d) :" << floor(d) << endl;
      cout << "sqrt(f) :" << sqrt(f) << endl;
      cout << "pow( d, 2) :" << pow(d, 2) << endl;

}
using std::setw;

void array_test (){
    double balance[10];
    
    double balance1[5] = {1000.0,2.0,3.4,7.0,50.0};
    
    double banlance2[] = {1000.0,2.0,3.4,7.0,50.0,89.0};
    
    int n[ 10 ]; // n 是一个包含 10 个整数的数组
    
    // 初始化数组元素
    for ( int i = 0; i < 10; i++ )
    {
        n[ i ] = i + 100; // 设置元素 i 为 i + 100
    }
    cout << "Element" << setw( 13 ) << "Value" << endl;
    
    // 输出数组中每个元素的值
    for ( int j = 0; j < 10; j++ )
    {
        cout << setw( 7 )<< j << setw( 13 ) << n[ j ] << endl;
    }
    
}
// 为了定义结构，您必须使用 struct 语句。struct 语句定义了一个包含多个成员的新的数据类型，struct 语句的格式如下：
//struct type_name {
//member_type1 member_name1;
//member_type2 member_name2;
//member_type3 member_name3;
//.
//.
//} object_names;
struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
} book;

void test_struct() {
    Books Book1;
    Books Book2;
    
    // 对Book1详述
    strcpy(Book1.title, "C++ 教程");
    strcpy(Book1.author, "Runoob");
    strcpy(Book1.subject, "编程语言");
    Book1.book_id = 12345;
    
    // 对Book2详述
    strcpy(Book2.title, "CSS 教程");
    strcpy(Book2.author, "Runoob");
    strcpy(Book2.subject, "前端技术");
    Book2.book_id = 12345;
    
    // 输出 Book1 信息
    cout << "第一本书标题 : " << Book1.title <<endl;
    cout << "第一本书作者 : " << Book1.author <<endl;
    cout << "第一本书类目 : " << Book1.subject <<endl;
    cout << "第一本书 ID : " << Book1.book_id <<endl;
    
    // 输出 Book2 信息
    cout << "第二本书标题 : " << Book2.title <<endl;
    cout << "第二本书作者 : " << Book2.author <<endl;
    cout << "第二本书类目 : " << Book2.subject <<endl;
    cout << "第二本书 ID : " << Book2.book_id <<endl;
}



struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
            val(x), next(NULL) {
    }
};

/**
 算法思想：逆置链表初始为空，表中节点从原链表节点中依次 “删除”，再逐个插入到逆置链表的表头（既头插到逆置链表中）
 使它成为逆置链表的“新”的第一个节点，如此循环，直至原链表为空
 */
class Solution {
public:
    ListNode* ReverseList(ListNode* pHead) {
       
        // 经过验证的
        ListNode *p, *q;
        // 原链表的新的头节点
        p=pHead->next;
        // 原链表的头结点的下一个节点为NULL
        pHead->next = NULL;
        while (p) {
            // 向后移动一个位置
            q=p;
            p=p->next;
            q->next = pHead;
            pHead = q;
        }
        

        return pHead;
    }
    
    ListNode* ReverseList2(ListNode* head) {
       
        ListNode* dumyOfInsertedList = new ListNode(-1);//一个哑结点，用于创建新的链表
            if(head == NULL) return NULL;
            
            ListNode* unInsertedHead = head;// 使用unInsertedHead和inInsertHead_next作为待逆置链表的首结点和次首结点的便签
            ListNode* unInsertedHead_next = head->next;
         
            while(unInsertedHead)// 头插直到原链表为空
            {
                unInsertedHead->next = dumyOfInsertedList->next;
                dumyOfInsertedList->next = unInsertedHead;
         
                unInsertedHead = unInsertedHead_next;
                if(unInsertedHead_next!=NULL)// 注意：最后一个结点时，unInsertedHead_next为NULL没有next成员变量
                {
                    //cout<<"head:"<< unInsertedHead->val<<"next:"<<unInsertedHead_next->val<<endl;
                    unInsertedHead_next = unInsertedHead_next->next;
                }
            }
         
            return dumyOfInsertedList->next;
    }
    
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
           // write code here
           if(l1==NULL && l2==NULL) {
               return NULL;
           }
           ListNode *temp3(0);
           ListNode *head = temp3;
           ListNode *temp1 = l1;
           ListNode *temp2 = l2;
           while(true) {
               if(temp2 == NULL) {
                   temp3->next = temp1;
                   break;
               } else if (temp1 == NULL) {
                   temp3->next = temp2;
                   break;
               } else if (temp1->val <= temp2->val) {
                   temp3->next = temp1;
                   temp1 = temp1->next;
                   temp3 = temp3->next;
               } else {
                   temp3->next = temp2;
                   temp2= temp2->next;
                   temp3 = temp3->next;
               }
           }
           return head->next;
       }
};


int main(int argc, const char * argv[]) {
    
    ListNode *node1 =new ListNode(0);
    ListNode *temp= node1;
    for (int i = 0;i < 20 ; i+=2) {
        
        ListNode *node = new ListNode(i);
    
        temp->next = node;
        
        temp = node;
        
    }
    
    cout << node1 << endl;
    
    
    test_struct();
    
    array_test();
    
    math_calculate();
    
    // insert code here...
    std::cout << "Hello, World!\n";
    cout << "Hello, CPP \n" ;
    cout << "type: \t\t" << "************size**************"<< endl;
    cout << "bool: \t\t" << "所占字节数：" << sizeof(bool);
    cout << "\t最大值：" << (numeric_limits<bool>::max)();
    cout << "\t\t最小值：" << (numeric_limits<bool>::min)() << endl;
    cout << "char: \t\t" << "所占字节数：" << sizeof(char);
    cout << "\t最大值：" << (numeric_limits<char>::max)();
    cout << "\t\t最小值：" << (numeric_limits<char>::min)() << endl;
    cout << "signed char: \t" << "所占字节数：" << sizeof(signed char);
    cout << "\t最大值：" << (numeric_limits<signed char>::max)();
    cout << "\t\t最小值：" << (numeric_limits<signed char>::min)() << endl;
    cout << "unsigned char: \t" << "所占字节数：" << sizeof(unsigned char);
    cout << "\t最大值：" << (numeric_limits<unsigned char>::max)();
    cout << "\t\t最小值：" << (numeric_limits<unsigned char>::min)() << endl;
    cout << "wchar_t: \t" << "所占字节数：" << sizeof(wchar_t);
    cout << "\t最大值：" << (numeric_limits<wchar_t>::max)();
    cout << "\t\t最小值：" << (numeric_limits<wchar_t>::min)() << endl;
    cout << "short: \t\t" << "所占字节数：" << sizeof(short);
    cout << "\t最大值：" << (numeric_limits<short>::max)();
    cout << "\t\t最小值：" << (numeric_limits<short>::min)() << endl;
    cout << "int: \t\t" << "所占字节数：" << sizeof(int);
    cout << "\t最大值：" << (numeric_limits<int>::max)();
    cout << "\t最小值：" << (numeric_limits<int>::min)() << endl;
    cout << "unsigned: \t" << "所占字节数：" << sizeof(unsigned);
    cout << "\t最大值：" << (numeric_limits<unsigned>::max)();
    cout << "\t最小值：" << (numeric_limits<unsigned>::min)() << endl;
    cout << "long: \t\t" << "所占字节数：" << sizeof(long);
    cout << "\t最大值：" << (numeric_limits<long>::max)();
    cout << "\t最小值：" << (numeric_limits<long>::min)() << endl;
    cout << "unsigned long: \t" << "所占字节数：" << sizeof(unsigned long);
    cout << "\t最大值：" << (numeric_limits<unsigned long>::max)();
    cout << "\t最小值：" << (numeric_limits<unsigned long>::min)() << endl;
    cout << "double: \t" << "所占字节数：" << sizeof(double);
    cout << "\t最大值：" << (numeric_limits<double>::max)();
    cout << "\t最小值：" << (numeric_limits<double>::min)() << endl;
    cout << "long double: \t" << "所占字节数：" << sizeof(long double);
    cout << "\t最大值：" << (numeric_limits<long double>::max)();
    cout << "\t最小值：" << (numeric_limits<long double>::min)() << endl;
    cout << "float: \t\t" << "所占字节数：" << sizeof(float);
    cout << "\t最大值：" << (numeric_limits<float>::max)();
    cout << "\t最小值：" << (numeric_limits<float>::min)() << endl;
    cout << "size_t: \t" << "所占字节数：" << sizeof(size_t);
    cout << "\t最大值：" << (numeric_limits<size_t>::max)();
    cout << "\t最小值：" << (numeric_limits<size_t>::min)() << endl;
    cout << "string: \t" << "所占字节数：" << sizeof(string) << endl;
    // << "\t最大值：" << (numeric_limits<string>::max)() << "\t最小值：" << (numeric_limits<string>::min)() << endl;
    cout << "type: \t\t" << "************size**************"<< endl;
    
    color c = red;
    
    cout << c << endl;
    
    int i,j;
    srand((unsigned) time( NULL ));
    for (i = 0; i < 10; i++) {
        j = rand();
        cout << "随机数：" << j << endl;
    }
    
    // 数字定义
    short  s;
    long   l;
    float  f;
    double d;
    
    // 数字赋值
    s = 10;
    i = 1000;
    l = 1000000;
    f = 230.47;
    d = 30949.374;
    
    // 数字输出
    cout << "short  s :" << s << endl;
    cout << "int    i :" << i << endl;
    cout << "long   l :" << l << endl;
    cout << "float  f :" << f << endl;
    cout << "double d :" << d << endl;

    
    
    return 0;
}
